package com.wujie.hello.spring.cloud.alibaba.nacos.rest.consumer.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.http.client.ClientHttpRequestInitializer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author wujie
 * @version 1.0.0:
 * @title TestController
 * @Description TODO
 * @dateTime 2020/9/5 18:01
 * @Copyright 2020-2021
 */
@RestController
public class TestController {
    @Autowired
    RestTemplate restTemplate;
    @Autowired
    LoadBalancerClient loadBalancerClient;
    @Value("${spring.application.name}")
    private String name;
    @RequestMapping("/hi")
    public String sayHi(){
        ServiceInstance choose = loadBalancerClient.choose("hello-spring-cloud-alibaba-nacos-provider");
        String url = String.format("http://%s:%s/hi?msg=%s",choose.getHost(),choose.getPort(),name);
        System.out.println("request uri:"+url);
        return restTemplate.getForObject(url,String.class);
    }
}
